package com.huidao.service;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import com.huidao.data.VIPDb;
import com.huidao.domain.OrderItem;
import com.huidao.domain.VIPCard;

/** 订单的操作逻辑 */
public class VipCardService {
	private static List<VIPCard> vipList =new ArrayList<VIPCard>();
	private static Scanner sc = new Scanner(System.in);
	private static List<OrderItem> list = BuyService.getList();
	private static BufferedReader br=VIPDb.getBr() ;
	private static BufferedWriter bw=VIPDb.getBw();
	private static List<String> listMsg = VIPDb.getList();
	private static VIPCard vipCard;
	static{
		try {
			getVIP();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**初始化信息
	 * @throws IOException */
	public static void getVIP() throws IOException{
		bw = new BufferedWriter(new FileWriter("VIP.txt"));
		br = new BufferedReader(new FileReader("VIP.txt"));
		String line =null;
		while((line = br.readLine())!=null){
			listMsg.add(line);
		}
	}
	/**查找是否已存在*/
	public static boolean vipCheck(String vipName,String vipCardId){
		if (vipName ==null) {
			System.out.println("输入错误，请重新输入");
			vipCard =null;
			return false;
		}
		for (String  string : listMsg) {
			String []str = string.split(":");
			if (vipName.equals(str[0])||vipCardId.equals(str[1])) {
				System.out.println("该用户存在");
				for (VIPCard vip : vipList) {
					if (vipName.equals(vip.getVipName())&&vipCardId.equals(vip.getVipCardId())) {
						vipCard =vip;
						return true;
					}
				}
			}
		}
		vipCard =null;
		return false;
	}
	/**注册*/
	public static boolean vipRegist(){
		System.out.println("请输入您注册的会员名字");
		String vipName = sc.next();
		System.out.println("请输入会员卡ID ");
		String vipCardId = sc.next();
		if (vipName ==null||vipCardId ==null) {
			System.out.println(" 输入错误，请重新输入");
		}
		if (vipCheck(vipName,vipCardId)) {
			System.out.println("请重新输入，用户名已存在");
			vipRegist();
			return false;
		}
		System.out.println("请输入您的电话号码");
			String vipTelNumber = sc.next();
			StringBuilder mes = new StringBuilder();
			mes.append(vipName).append(":").append(vipCardId).append(":").append(vipTelNumber);
			try {
				bw.write(mes.toString());
				bw.newLine();
				bw.flush();			
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		System.out.println("请输入您充值的金额");
		double vipSurplusMoney = sc.nextDouble();
		VIPCard vipcard = new VIPCard(vipCardId ,vipName,vipTelNumber,vipSurplusMoney,0);
		listMsg.add(mes.toString());
		vipList.add(vipcard);
		System.out.println("注册成功");
		vipCard = vipcard;
//		System.out.println(vipCard.toString());
		return true;
	}
	/**登录*/
	public static boolean vipLogin(String vipName,String vipCardId){
		for (String  string : listMsg) {
			String []str = string.split(":");
			if (vipName.equals(str[0])&&vipCardId.equals(str[1])) {
				System.out.println("登录成功");
				return true;
			}
		}
		return false;
	}
	/**会员卡充值*/
	public boolean Recharge(){
		System.out.println("请输入您注册的会员名字");
		String vipName = sc.next();
		System.out.println("请输入会员卡ID ");
		String vipCardId = sc.next();
		if (new VipCardService().vipLogin(vipName, vipCardId)) {
			vipCheck(vipName, vipCardId);
			System.out.println("请输入充值的金额：");
			vipCard.setVipSurplusMoney(vipCard.getVipSurplusMoney()+sc.nextInt());
			System.out.println("您Vip卡上余额："+vipCard.getVipSurplusMoney());
			return true;
		}
		return false;
	}
	/**删除会员*/
	@SuppressWarnings("unchecked")
	public boolean vipDel(){
		List<VIPCard> newVipList =new ArrayList<VIPCard>();
		System.out.println("请输入您要删除该会员注册时的名字");
		String vipName = sc.next();
		System.out.println("请输入您要删除该会员注册时的会员卡ID ");
		String vipCardId = sc.next();
		if (!(vipCheck(vipName, vipCardId))) {
			return false;
		}
		for (VIPCard  vip : vipList) {
			if (vipCard.equals(vip)) {
				continue;
			}
			newVipList = (List<VIPCard>) vip;
		}
		vipList=newVipList;
		return true;
	}
	//求订单总价
	public static double getTotalPrice(){
		double total= 0;
		for (OrderItem orderItem : list) {
			total +=orderItem.getCount()*orderItem.getFood(). getPrice();
		}
		return total;
	}
	/**
	 * @return the vipCard
	 */
	public static VIPCard getVipCard() {
		return vipCard;
	}
//	public static void main(String[] args) {
//		new VipCardService(). vipRegist();
//		System.out.println(new VipCardService(). getVipCard() .toString());
//	}
}
